package com.qx.frame.panel;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.dom4j.Document;
import org.dom4j.Element;

import com.qx.frame.button.LButton;
import com.qx.iconst.PathConst;
import com.qx.util.AppFunction;
import com.qx.util.SwingUtils;

/**
 * 描述：<br>
 * 功能模块
 * 
 * @author LazyBear
 * @data 2014-4-22
 */
public class FunctionPanel extends JPanel {

	private static final long serialVersionUID = -1557374350036398947L;

	/**
	 * 功能模块实体
	 */
	private static FunctionPanel instance;
	/**
	 * xml读取对象
	 */
	private static Document document;
	/**
	 * 滑动条Panel对象
	 */
	private static JScrollPane scrollPane;

	public static JScrollPane getInstance() {
		if (instance == null)
			instance = new FunctionPanel();
		if (scrollPane == null) {
			scrollPane = new JScrollPane(instance);
			scrollPane.setBounds(0, 0, 985, 485);
			instance.setPreferredSize(new Dimension(scrollPane.getWidth() - 50, scrollPane.getHeight()
					* document.getRootElement().elements().size() / 75));
		}
		return scrollPane;
	}

	private FunctionPanel() {
		try {
			document = AppFunction.readXml(PathConst.function_cfg);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		SwingUtils.initPanel(this);
		this.setLayout(new FlowLayout());
		initButtons();
		this.setVisible(true);
	}

	/**
	 * 初始化功能按钮
	 * 
	 * @author LazyBear 描述:<br>
	 */
	private void initButtons() {
		List<Element> elements = document.getRootElement().elements();
		for (int i = 0; i < elements.size(); i++) {
			LButton functionButton = new LButton(Integer.valueOf(elements.get(i).attributeValue("index")), elements.get(i).attributeValue(
					"name"));
			functionButton.setName(elements.get(i).attributeValue("code"));
			// if (elements.get(i).attributeValue("type").equals("1")) {
			//
			// System.out.println(elements.get(i).attributeValue("name"));
			// } else {

			// }
			this.add(functionButton);
		}
	}
}
